package com.zlz.blog.server.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.LinkedList;

/**
 * @author wb_zhulinzhong
 * @date 2021-05-21 15:48:29
 */
public class ImageUtil {

    public static String getImageSegmentation(){
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = "AKIDrWw6PSFm21jCNHuHOEnESP5VgZBWbQyE";
        String secretKey = "1lEQKyMULvFI4ZnkQpyqQjmYEemeFciX";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-beijing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);

        // 存储桶的命名格式为 BucketName-APPID，此处填写的存储桶名称必须为此格式
        String bucketName = "zlzblog-1259290575";
        String key = "zjl.jpeg";
        GeneratePresignedUrlRequest req =
                new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
        // 设置签名过期时间(可选), 若未进行设置, 则默认使用 ClientConfig 中的签名过期时间(1小时)
        // 这里设置签名在半个小时后过期
        Date expirationDate = new Date(System.currentTimeMillis() + 30L * 60L * 1000L);
        req.setExpiration(expirationDate);
        req.addRequestParameter("type", "face-segmentation");
        req.addRequestParameter("ci-process", "face-effect");
        URL url = cosClient.generatePresignedUrl(req);
        cosClient.shutdown();
        return url.toString();
    }

//    public static void persistenceImage(COSClient cosClient) {
//        // bucket名需包含appid
//        // api 请参考 https://cloud.tencent.com/document/product/436/54050
//        String bucketName = "examplebucket-1250000000";
//
//        String key = "test.jpg";
//        File localFile = new File("E://test.jpg");
//        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
//        PicOperations picOperations = new PicOperations();
//        picOperations.setIsPicInfo(1);
//        List<PicOperations.Rule> ruleList = new LinkedList<>();
//        PicOperations.Rule rule1 = new PicOperations.Rule();
//        rule1.setBucket(bucketName);
//        rule1.setFileId("test-1.jpg");
//        rule1.setRule("imageMogr2/rotate/90");
//        ruleList.add(rule1);
//        PicOperations.Rule rule2 = new PicOperations.Rule();
//        rule2.setBucket(bucketName);
//        rule2.setFileId("test-2.jpg");
//        rule2.setRule("imageMogr2/rotate/180");
//        ruleList.add(rule2);
//        picOperations.setRules(ruleList);
//        putObjectRequest.setPicOperations(picOperations);
//        try {
//            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
//            CIUploadResult ciUploadResult = putObjectResult.getCiUploadResult();
//            System.out.println(putObjectResult.getRequestId());
//            System.out.println(ciUploadResult.getOriginalInfo().getEtag());
//            for(CIObject ciObject:ciUploadResult.getProcessResults().getObjectList()) {
//                System.out.println(ciObject.getLocation());
//                System.out.println(ciObject.getEtag());
//            }
//        } catch (CosServiceException e) {
//            e.printStackTrace();
//        } catch (CosClientException e) {
//            e.printStackTrace();
//        }
//    }
}
